<!--
  * 用户绑定关系列表
  *
  * @Author:    kaiyun
  * @Date:      2024-06-23 23:31:38
  * @Copyright  <a href="https://1024lab.net">1024创新实验室</a>
-->
<template>
  <!---------- 查询表单form begin ----------->  
  <a-form class="smart-query-form">
    <a-row class="smart-query-form-row">
      <a-form-item label="用户ID" class="smart-query-form-item">
        <a-input style="width: 200px" v-model:value="queryForm.userId" placeholder="请输入用户ID" />
      </a-form-item>
      <a-form-item label="用户昵称" class="smart-query-form-item">
        <a-input style="width: 200px" v-model:value="queryForm.nickName" placeholder="请输入用户昵称" />
      </a-form-item>
      <a-form-item label="绑定状态" class="smart-query-form-item">
        <a-select v-model:value="queryForm.bindStatus" style="width: 200px" placeholder="请选择绑定状态">
          <a-select-option :value="1">已绑定</a-select-option>
          <a-select-option :value="0">未绑定</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item class="smart-query-form-item">
        <a-button-group>
          <a-button type="primary" @click="onSearch">
            <template #icon>
              <SearchOutlined />
            </template>
            查询
          </a-button>
          <a-button @click="resetQuery">
            <template #icon>
              <ReloadOutlined />
            </template>
            重置
          </a-button>
        </a-button-group>
      </a-form-item>
    </a-row>
  </a-form>
  <!---------- 查询表单form end ----------->  

  <a-card size="small" :bordered="false" :hoverable="true">
    <!---------- 表格 begin ----------->  
    <a-table
      :columns="columns"
      :data-source="tableData"
      :loading="tableLoading"
      :pagination="{
        total: total,
        current: queryForm.pageNum,
        pageSize: queryForm.pageSize,
        showSizeChanger: true,
        showQuickJumper: true,
        showTotal: (total) => `共 ${total} 条`,
        onChange: changePage,
      }"
      :scroll="{ x: 800 }"
      row-key="userId"
    >
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'userAvatar'">
          <a-image
            :src="record.userAvatar || '/avatar-default.png'"
            :preview="false"
            :width="40"
            :height="40"
            style="border-radius: 50%"
          />
        </template>
        <template v-if="column.dataIndex === 'bindStatus'">
          <a-tag :color="record.bindStatus === 1 ? 'green' : 'red'">
            {{ record.bindStatus === 1 ? '已绑定' : '未绑定' }}
          </a-tag>
        </template>
        <template v-if="column.dataIndex === 'action'">
          <a-button type="link" @click="handleUnbind(record)" :disabled="record.bindStatus === 0">
            解绑
          </a-button>
        </template>
      </template>
    </a-table>
    <!---------- 表格 end ----------->  
  </a-card>
</template>

<script setup>
  import { ref, reactive, onMounted } from 'vue';
  import { SearchOutlined, ReloadOutlined } from '@ant-design/icons-vue';
  import { defaultTimeRanges } from '/@/lib/default-time-ranges';
  import { message, Modal } from 'ant-design-vue';
  import { userBindApi } from '/@/api/business/bjd/user-bind-api';

  // --------------------------- 表格列定义 ---------------------------
  const columns = ref([
    {
      title: '绑定ID',
      dataIndex: 'id',
      width: 150,
      ellipsis: true,
    },
    {
      title: '订单编号',
      dataIndex: 'orderNo',
      width: 150,
      ellipsis: true,
    },
    // {
    //   title: '用户昵称',
    //   dataIndex: 'userNickName',
    //   width: 150,
    //   ellipsis: true,
    // },
    // {
    //   title: '头像',
    //   dataIndex: 'userAvatar',
    //   width: 80,
    // },
    {
      title: 'OpenID',
      dataIndex: 'userOpenId',
      width: 200,
      ellipsis: true,
    },
    {
      title: '娃娃ID',
      dataIndex: 'dollNo',
      width: 200,
      ellipsis: true,
    },
    {
      title: '绑定状态',
      dataIndex: 'bindStatus',
      width: 100,
    },
    {
      title: '操作',
      dataIndex: 'action',
      width: 100,
      fixed: 'right',
    },
  ]);

  // --------------------------- 查询相关 ---------------------------
  const queryFormState = {
    userId: undefined,
    nickName: undefined,
    bindStatus: undefined,
    pageNum: 1,
    pageSize: 10,
  };

  const queryForm = reactive({ ...queryFormState });
  const tableLoading = ref(false);
  const tableData = ref([]);
  const total = ref(0);

  // 查询数据
  function onSearch() {
    queryForm.pageNum = 1;
    queryData();
  }

  // 重置查询
  function resetQuery() {
    Object.assign(queryForm, queryFormState);
    queryData();
  }

  // 分页改变
  function changePage(page, pageSize) {
    queryForm.pageNum = page;
    queryForm.pageSize = pageSize;
    queryData();
  }

  // 查询数据方法
  async function queryData() {
    try {
      tableLoading.value = true;
      const result = await userBindApi.queryPage(queryForm);
      tableData.value = result.data.list;
      total.value = result.data.total;
    } catch (err) {
      console.error(err);
      message.error('查询失败');
    } finally {
      tableLoading.value = false;
    }
  }

  // 解绑操作
  function handleUnbind(record) {
    Modal.confirm({
      title: '确认解绑',
      content: `确定要解绑用户 ${record.userNickName} 吗？`,
      async onOk() {
        try {
          const param = {
            id: record.id,
          };
          await userBindApi.unbind(param);
          message.success('解绑成功');
          queryData();
        } catch (err) {
          console.error(err);
          message.error('解绑失败');
        }
      },
    });
  }

  onMounted(() => {
    queryData();
  });
</script>